using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Describes a connection to a data source.  This is typically a transactional database.
	/// </summary>
   public class DataSourceDetail : EntityBase
   {
		protected System.String mName;
		protected string mLocation;
		protected string mSchema;
		protected string mUser;
		protected string mPassword;
		protected string mDescription;

		public DataSourceDetail()
		{
	
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// Specifies the server, machine or cluster name that the datasource resides on.
		/// </summary>
		public string Location
		{
			get
			{
				return ( mLocation );
			}
			set
			{
				mLocation = value;
			}
		}

		/// <summary>
		/// Defines the database instance or schema for the datasource.
		/// </summary>
		public string Schema
		{
			get
			{
				return ( mSchema );
			}
			set
			{
				mSchema = value;
			}
		}

		/// <summary>
		/// Specifies the user name for connections to the datasource.
		/// </summary>
		public string User
		{
			get
			{
				return ( mUser );
			}
			set
			{
				mUser = value;
			}
		}

		/// <summary>
		/// Specifies the password to be used in connections to the datasource.
		/// </summary>
		public string Password
		{
			get
			{
				return ( mPassword );
			}
			set
			{
				mPassword = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mLocation = DataUtility.GetString( pSource, "location" );
			mSchema = DataUtility.GetString( pSource, "schema" );
			mUser = DataUtility.GetString( pSource, "user" );
			mPassword = DataUtility.GetString( pSource, "password" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "datasource", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mLocation ) )
			{
				vNewElement.SetAttribute( "location", mLocation );
			}
					
			if ( !String.IsNullOrEmpty( mSchema ) )
			{
				vNewElement.SetAttribute( "schema", mSchema );
			}
					
			if ( !String.IsNullOrEmpty( mUser ) )
			{
				vNewElement.SetAttribute( "user", mUser );
			}
					
			if ( !String.IsNullOrEmpty( mPassword ) )
			{
				vNewElement.SetAttribute( "password", mPassword );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.DataSourceDetail v_oReturn = new Derived.DataSourceDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Location = mLocation;
			v_oReturn.Schema = mSchema;
			v_oReturn.User = mUser;
			v_oReturn.Password = mPassword;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.DataSourceDetail CloneTyped()
		{
			return ( ( Derived.DataSourceDetail )this.Clone() );
		}
   }
}
   